using System.Collections.Generic;
using System.Linq;
using CoreGraphics;
using Foundation;
using mb;
using mb.iOS;
using Masonry;
using MyExtensions;
using Shared;
using UIKit;
using Xamarin.Forms.Platform.iOS;

namespace libmp
{
    class VCOfflineCacheController : VCBaseInsertableList<DataStore.VideoItem>
    {
        UIView titleBar;
        int HeaderHeight = 48;
        string userId;
        List < (int vid, long total, long now) > offlineItemInfoList;

        public VCOfflineCacheController()
        {
            userId = User.me.id;
        }

        public override void ViewDidLoad()
        {
            var bkView = new UIView() { BackgroundColor = Util.MainBKColor };
            bkView.SetParent(View).MakeConstraints(m =>
            {
                m.Top.EqualTo(this.TopLayoutGuideBottom());
                m.Left.Right.Bottom.EqualTo(View);
            });
            base.ViewDidLoad();

            SetTitleBar("离线缓存");

            listView.RemakeConstraints(m =>
            {
                m.Left.Right.EqualTo(View);
                m.Top.EqualTo(this.TopLayoutGuideBottom()).Offset(HeaderHeight);
                m.Bottom.EqualTo(this.BottomLayoutGuideTop());
            });

            offlineItemInfoList = GlobalImpl.VideoServer.GetOfflineItemInfoList();
            dataSource = new List<DataStore.VideoItem>();
            foreach (var item in offlineItemInfoList)
            {
                dataSource.Add(DataStore.store.GetVideoItemById(item.vid));
            }

            this.Reload();
        }

        void SetTitleBar(string titleName)
        {
            titleBar = new UIView()
            {
                BackgroundColor = Util.MainColor,
            };
            titleBar.SetParent(View);
            titleBar.Frame = new CGRect(0, 0, View.Frame.Size.Width, 48);
            titleBar.MakeConstraints(m =>
            {
                m.Top.EqualTo(this.TopLayoutGuideBottom());
                m.Width.Left.EqualTo(View);
                m.Height.EqualTo(NSObject.FromObject(HeaderHeight));
            });
            var btnPicWidth = 15;
            var btnPicHeight = 15;

            var topBarLeftView = new UIView();
            topBarLeftView.SetParent(titleBar);
            topBarLeftView.MakeConstraints(m =>
            {
                m.Left.EqualTo(NSObject.FromObject(10));
                m.Top.EqualTo(titleBar);
                m.Bottom.EqualTo(titleBar);
                m.Width.EqualTo(NSObject.FromObject((View.Bounds.Width - 20) / 3));
            });

            var leftButton1 = new UIButton();
            leftButton1.Frame = new CGRect(0, 14, btnPicWidth + 30, btnPicHeight + 5);
            leftButton1.SetParent(topBarLeftView);
            var leftButton1ImageView = new UIImageView();
            var imgoperationArrow1 = UIImage.FromFile($"{App.HotStorageRoot}/res/user/Fanhui.png");
            leftButton1ImageView.Image = new UIImage(imgoperationArrow1.CGImage, 2, UIImageOrientation.Up);
            leftButton1ImageView.SetParent(leftButton1);
            leftButton1ImageView.MakeConstraints(m =>
            {
                m.CenterY.EqualTo(leftButton1.CenterY());
                m.Left.EqualTo(NSObject.FromObject(5));
                m.Height.EqualTo(NSObject.FromObject(btnPicHeight));
                m.Width.EqualTo(NSObject.FromObject(btnPicWidth / 2));
            });
            var leftButton2Label = new UILabel();
            leftButton2Label.Text = "返回";
            leftButton2Label.Font = UIFont.SystemFontOfSize(12);
            leftButton2Label.SetParent(leftButton1);
            leftButton2Label.TextColor = UIColor.White;
            leftButton2Label.MakeConstraints(m =>
            {
                m.CenterY.EqualTo(leftButton1.CenterY());
                m.Left.EqualTo(NSObject.FromObject(20));
                m.Height.EqualTo(NSObject.FromObject(btnPicHeight));
                m.Width.EqualTo(NSObject.FromObject(btnPicWidth + 20));
            });
            leftButton1.TouchUpInside += (s, e) =>
            {
                logger.Log("返回====>");
                AppDelegate.Instance.NavController.PopViewController(false);
            };

            var topBarMiddleView = new UIView();
            topBarMiddleView.SetParent(titleBar);
            topBarMiddleView.MakeConstraints(m =>
            {
                m.Left.EqualTo(topBarLeftView.Right());
                m.Top.EqualTo(titleBar);
                m.Bottom.EqualTo(titleBar);
                m.Width.EqualTo(NSObject.FromObject((View.Bounds.Width - 20) / 3));
            });

            var middleLabel = new UILabel();
            middleLabel.Text = titleName;
            middleLabel.Font = UIFont.BoldSystemFontOfSize(18);
            middleLabel.SetParent(topBarMiddleView);
            middleLabel.TextColor = UIColor.White;
            middleLabel.MakeConstraints(m =>
            {
                m.CenterY.EqualTo(topBarMiddleView.CenterY());
                m.CenterX.EqualTo(topBarMiddleView.CenterX());
            });

            var topBarRightView = new UIView();
            topBarRightView.SetParent(titleBar);
            topBarRightView.MakeConstraints(m =>
            {
                m.Left.EqualTo(topBarMiddleView.Right());
                m.Top.EqualTo(titleBar);
                m.Bottom.EqualTo(titleBar);
                m.Width.EqualTo(NSObject.FromObject((View.Bounds.Width - 20) / 3));
            });
        }

        protected class MovieCellData : MyCellData
        {
            public Shared.OfflineItemPage page;
            public MovieCellData(MyCollectionViewCell cell) : base(cell)
            {
                var cellView = new UIView();
                cellView.SetParent(cell);
                cellView.Frame = new CGRect(10, 10, cell.Bounds.Width - 20, cell.Bounds.Height - 10);
                cellView.Layer.CornerRadius = 10;
                cellView.Layer.BorderWidth = 1;
                cellView.Layer.BorderColor = new CGColor(0.3f, 0.3f, 0.3f, 1f);
                cellView.ClipsToBounds = true;
                cellView.BackgroundColor = UIColor.White;

                page = new OfflineItemPage(
                    onPress: () =>
                    {

                    },
                    onLongPress: () =>
                    {

                    }
                );
                var fvc = page.CreateViewController();
                fvc.View.SetParent(cellView);
                fvc.View.MakeConstraints(m => m.Edges.EqualTo(cellView));
            }
        }
        protected override MyCellData CreateCellData(MyCollectionViewCell cell, SectionData sd)
        {
            return new MovieCellData(cell);
        }
        protected override void UpdateCell(MyCollectionViewCell cell, int sec, int idx)
        {
            var cd = cell.data as MovieCellData;
            var sd = sections[sec];
            var vi = dataSource[sd.dataStartIndex + idx];
            cd.page.Update(vi, offlineItemInfoList[sd.dataStartIndex + idx]);

        }
    }
}